// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.prefs.connectivity.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.dengues.commons.connectivity.EDatabaseType;
import org.dengues.commons.ui.swt.viewers.DirectoryCellEditor;
import org.dengues.commons.ui.swt.viewers.FileCellEditor;
import org.dengues.ui.prefs.connectivity.EConnectivityProperties;
import org.dengues.ui.prefs.connectivity.models.ConnectivityCategory;
import org.dengues.ui.prefs.connectivity.models.ConnectivityProperty;
import org.dengues.ui.prefs.connectivity.models.ConnectivitySubCategory;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class ConnectivityPropertySource implements IPropertySource {

    public final static String PROP_ID_URL = EConnectivityProperties.PROP_ID_URL.getPropId();

    public final static String PROP_ID_USER = EConnectivityProperties.PROP_ID_USER.getPropId();

    public final static String PROP_ID_PASSWORD = EConnectivityProperties.PROP_ID_PASSWORD.getPropId();

    public final static String PROP_ID_DRIVER = EConnectivityProperties.PROP_ID_DRIVER.getPropId();

    private final static String PROP_ID_DATABASE = EConnectivityProperties.PROP_ID_DATABASE.getPropId();

    private final static String PROP_ID_SCHEMA = EConnectivityProperties.PROP_ID_SCHEMA.getPropId();

    private final static String PROP_ID_PORT = EConnectivityProperties.PROP_ID_PORT.getPropId();

    private final static String PROP_ID_HOST = EConnectivityProperties.PROP_ID_HOST.getPropId();

    private static final String PROP_ID_ROOTPATH = EConnectivityProperties.PROP_ID_ROOTPATH.getPropId();

    private static final String PROP_ID_FILE = EConnectivityProperties.PROP_ID_FILE.getPropId();

    private ConnectivityCategory category;

    private DriverClassesPropertyDescriptor driverClasses;

    private String defaultUrl;

    private String defaultPort;

    private ConnectivityProperty property;

    private Properties properties;

    private String defaultDriver;

    private List<String> driverClassesString;

    private ArrayList<IPropertyDescriptor> propertyDescriptors;

    private final List<String> idProperties;

    private Boolean isNeedUser;

    /**
     * Qiang.Zhang.Adolf@gmail.com ConnectivityPropertySource constructor comment.
     * 
     * @param object
     */
    public ConnectivityPropertySource(ConnectivityCategory object) {
        super();
        idProperties = new ArrayList<String>();
        if (object instanceof ConnectivityProperty) {
            property = (ConnectivityProperty) object;
            this.properties = (Properties) property.getBaseProperties().clone();
            init(property.getParent());
        } else {
            this.properties = new Properties();
            init(object);
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "init".
     * 
     * @param category
     */
    private void init(ConnectivityCategory category) {
        this.category = category;
        if (category instanceof ConnectivitySubCategory) {
            this.defaultUrl = ((ConnectivitySubCategory) category).getSubType().getUrl();
            this.defaultPort = ((ConnectivitySubCategory) category).getSubType().getPort();
            this.defaultDriver = ((ConnectivitySubCategory) category).getSubType().getDriver();
            isNeedUser = ((ConnectivitySubCategory) category).getSubType().isNeedUser();
        } else {
            this.defaultUrl = category.getType().getUrl();
            this.defaultPort = category.getType().getPort();
            this.defaultDriver = category.getType().getDriver();
            isNeedUser = category.getType().isNeedUser();
        }
        driverClassesString = new ArrayList<String>();
        driverClassesString.add(defaultDriver);
        if (!properties.containsKey(PROP_ID_URL)) {
            properties.setProperty(PROP_ID_URL, defaultUrl);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
     */
    public Object getEditableValue() {
        return properties;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
     */
    public IPropertyDescriptor[] getPropertyDescriptors() {
        if (propertyDescriptors == null || propertyDescriptors.size() == 0) {
            propertyDescriptors = new ArrayList<IPropertyDescriptor>();

            driverClasses = new DriverClassesPropertyDescriptor(PROP_ID_DRIVER, EConnectivityProperties.PROP_ID_DRIVER
                    .getDisplayName(), category.getDriverClasses());
            propertyDescriptors.add(driverClasses);
            if (!properties.containsKey(PROP_ID_DRIVER)) {
                properties.setProperty(PROP_ID_DRIVER, defaultDriver);
            }
            counntURL(propertyDescriptors);
            countDatabase(propertyDescriptors);
            countSchema(propertyDescriptors);

            countHost(propertyDescriptors);
            countPort(propertyDescriptors);

            countUser(propertyDescriptors);
            countPassword(propertyDescriptors);

            countRootPath(propertyDescriptors);
            countFile(propertyDescriptors);
            setUrl();
        }
        return propertyDescriptors.toArray(new IPropertyDescriptor[0]);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "countSchema".
     * 
     * @param list
     */
    private void countSchema(List<IPropertyDescriptor> list) {
        if (category.getType().isNeedSchema()) {
            IPropertyDescriptor descriptor = new TextPropertyDescriptor(PROP_ID_SCHEMA, EConnectivityProperties.PROP_ID_SCHEMA
                    .getDisplayName());
            list.add(descriptor);
            if (!properties.containsKey(PROP_ID_SCHEMA)) {
                properties.setProperty(PROP_ID_SCHEMA, ""); //$NON-NLS-1$
            }
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "countPort".
     * 
     * @param list
     */
    private void countPort(List<IPropertyDescriptor> list) {
        if (defaultUrl.contains(EDatabaseType.MARK_PORT)) {
            IPropertyDescriptor descriptor = new TextPropertyDescriptor(PROP_ID_PORT, EConnectivityProperties.PROP_ID_PORT
                    .getDisplayName());
            list.add(descriptor);
            if (!properties.containsKey(PROP_ID_PORT)) {
                properties.setProperty(PROP_ID_PORT, defaultPort);
            }
            idProperties.add(PROP_ID_PORT);
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "countHost".
     * 
     * @param list
     */
    private void countHost(List<IPropertyDescriptor> list) {
        if (defaultUrl.contains(EDatabaseType.MARK_HOST)) {
            IPropertyDescriptor descriptor = new TextPropertyDescriptor(PROP_ID_HOST, EConnectivityProperties.PROP_ID_HOST
                    .getDisplayName());
            list.add(descriptor);
            if (!properties.containsKey(PROP_ID_HOST)) {
                properties.setProperty(PROP_ID_HOST, "localhost"); //$NON-NLS-1$
            }
            idProperties.add(PROP_ID_HOST);
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "countDBFile".
     * 
     * @param list
     */
    private void countFile(List<IPropertyDescriptor> list) {
        if (defaultUrl.contains(EDatabaseType.MARK_FILE)) {
            IPropertyDescriptor descriptor = new TextPropertyDescriptor(PROP_ID_FILE, EConnectivityProperties.PROP_ID_FILE
                    .getDisplayName()) {

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.views.properties.TextPropertyDescriptor#createPropertyEditor(org.eclipse.swt.widgets.Composite)
                 */
                @Override
                public CellEditor createPropertyEditor(Composite parent) {
                    FileCellEditor cellEditor = new FileCellEditor(parent);
                    return cellEditor;
                }
            };
            list.add(descriptor);
            if (!properties.containsKey(PROP_ID_FILE)) {
                properties.setProperty(PROP_ID_FILE, ""); //$NON-NLS-1$
            }
            idProperties.add(PROP_ID_FILE);
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "countDBDirectory".
     * 
     * @param list
     */
    private void countRootPath(List<IPropertyDescriptor> list) {
        if (defaultUrl.contains(EDatabaseType.MARK_ROOTPATH)) {
            IPropertyDescriptor descriptor = new TextPropertyDescriptor(PROP_ID_ROOTPATH,
                    EConnectivityProperties.PROP_ID_ROOTPATH.getDisplayName()) {

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.views.properties.TextPropertyDescriptor#createPropertyEditor(org.eclipse.swt.widgets.Composite)
                 */
                @Override
                public CellEditor createPropertyEditor(Composite parent) {
                    DirectoryCellEditor cellEditor = new DirectoryCellEditor(parent);
                    return cellEditor;
                }

            };
            list.add(descriptor);
            if (!properties.containsKey(PROP_ID_ROOTPATH)) {
                properties.setProperty(PROP_ID_ROOTPATH, ""); //$NON-NLS-1$
            }
            idProperties.add(PROP_ID_ROOTPATH);
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "countDatabase".
     * 
     * @param list
     */
    private void countDatabase(List<IPropertyDescriptor> list) {
        if (defaultUrl.contains(EDatabaseType.MARK_SID)) {
            IPropertyDescriptor descriptor;
            descriptor = new TextPropertyDescriptor(PROP_ID_DATABASE, EConnectivityProperties.PROP_ID_DATABASE.getDisplayName());
            list.add(descriptor);
            if (!properties.containsKey(PROP_ID_DATABASE)) {
                properties.setProperty(PROP_ID_DATABASE, ""); //$NON-NLS-1$
            }
            idProperties.add(PROP_ID_DATABASE);
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "countPassword".
     * 
     * @param list
     */
    private void countPassword(List<IPropertyDescriptor> list) {
        if (isNeedUser) {
            IPropertyDescriptor descriptor = new PasswordPropertyDescriptor(PROP_ID_PASSWORD,
                    EConnectivityProperties.PROP_ID_PASSWORD.getDisplayName());
            list.add(descriptor);
            if (!properties.containsKey(PROP_ID_PASSWORD)) {
                properties.setProperty(PROP_ID_PASSWORD, ""); //$NON-NLS-1$
            }
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "countUser".
     * 
     * @param list
     */
    private void countUser(List<IPropertyDescriptor> list) {
        if (isNeedUser) {
            IPropertyDescriptor descriptor = new TextPropertyDescriptor(PROP_ID_USER, EConnectivityProperties.PROP_ID_USER
                    .getDisplayName());
            list.add(descriptor);
            if (!properties.containsKey(PROP_ID_USER)) {
                properties.setProperty(PROP_ID_USER, ""); //$NON-NLS-1$
            }
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "counntURL".
     * 
     * @param list
     */
    private void counntURL(List<IPropertyDescriptor> list) {
        IPropertyDescriptor descriptor = new TextPropertyDescriptor(PROP_ID_URL, EConnectivityProperties.PROP_ID_URL
                .getDisplayName());
        list.add(descriptor);
        if (!properties.containsKey(PROP_ID_URL)) {
            properties.setProperty(PROP_ID_URL, ""); //$NON-NLS-1$
        }
    }

    public void setDriverClasses(List<String> classes) {
        driverClassesString.clear();
        driverClassesString.add(defaultDriver);
        driverClassesString.addAll(classes);
        driverClasses.setItems(driverClassesString.toArray(new String[0]));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
     */
    public Object getPropertyValue(Object id) {
        if (id.equals(PROP_ID_DRIVER)) {
            Object object = getProperties().get(id);
            return driverClassesString.indexOf(object);
        }
        return properties.get(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
     */
    public boolean isPropertySet(Object id) {
        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
     */
    public void resetPropertyValue(Object id) {
        // Do nothing
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
     */
    public void setPropertyValue(Object id, Object value) {
        String value2;
        if (id.equals(PROP_ID_DRIVER)) {
            value2 = driverClassesString.get((Integer) value);
        } else {
            value2 = (String) value;
        }
        getProperties().setProperty((String) id, value2);
        setUrl();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setUrl".
     * 
     * @param id
     * @param value2
     */
    private void setUrl() {
        String url = defaultUrl;
        if (idProperties.contains(PROP_ID_DATABASE)) {
            url = url.replaceAll(EDatabaseType.MARK_SID, getProperties().getProperty(PROP_ID_DATABASE));
        }
        if (idProperties.contains(PROP_ID_FILE)) {
            url = url.replaceAll(EDatabaseType.MARK_FILE, getProperties().getProperty(PROP_ID_FILE));
        }
        if (idProperties.contains(PROP_ID_HOST)) {
            url = url.replaceAll(EDatabaseType.MARK_HOST, getProperties().getProperty(PROP_ID_HOST));
        }
        if (idProperties.contains(PROP_ID_PORT)) {
            url = url.replaceAll(EDatabaseType.MARK_PORT, getProperties().getProperty(PROP_ID_PORT));
        }
        if (idProperties.contains(PROP_ID_ROOTPATH)) {
            url = url.replaceAll(EDatabaseType.MARK_ROOTPATH, getProperties().getProperty(PROP_ID_ROOTPATH));
        }
        getProperties().setProperty(PROP_ID_URL, url);
    }

    public Properties getProperties() {
        return this.properties;
    }

}
